package cn.jiyun.controller;

import cn.jiyun.page.PageResult;
import cn.jiyun.page.QueryPageBean;
import cn.jiyun.pojo.TaskPlan;
import cn.jiyun.pojo.Project;
import cn.jiyun.service.TaskPlanService;
import cn.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("taskPlan")
public class TaskPlanController {
    @Autowired
    private TaskPlanService taskPlanService;

    @RequestMapping("add")
    public Result add(@RequestBody Map<String,String> taskPlan){
        int add = taskPlanService.add(taskPlan);
        if (add>0){
            return new Result(true,"success");
        }else {
            return new Result(false,"file");
        }
    }
    @RequestMapping("update")
    public Result update(@RequestBody  TaskPlan taskPlan){
        int add = taskPlanService.update(taskPlan);
        if (add>0){
            return new Result(true,"success");
        }else {
            return new Result(false,"file");
        }
    }

    @RequestMapping("findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
     return taskPlanService.findAll(queryPageBean);
    }

    @RequestMapping("delete")
    public Result delete(Integer did){
        int add = taskPlanService.delete(did);
        if (add>0){
            return new Result(true,"success");
        }else {
            return new Result(false,"file");
        }
    }

    @RequestMapping("findById")
    public Result findById(Integer did){
        TaskPlan byId = taskPlanService.findById(did);
        return new Result(true,"成功",byId);
    }

    @RequestMapping("findProjectList")
    public Result findProjectList(Integer did){
        List<Project> list = taskPlanService.findProjectList(did);
        List<Project> list2 = taskPlanService.findProjectList2();
        for (int i = 0; i < list2.size(); i++) {
            Project project = list2.get(i);
            list.add(project);
        }
        return new Result(true,"成功",list);
    }

    @RequestMapping("findIds")
    public Result findIds(Integer did){

        List<Project> list = taskPlanService.findProjectList(did);
        int[] ids=new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Project project = list.get(i);
            Integer pid = project.getPid();
            ids[i]=pid;
        }
        return new Result(true,"成功",ids);
    }

}
